作者:VB.NET开发
项目:Syste
Public Class SamplesArray
Public Shared Sub Main()
' Create and initialize a new string array.
Dim myArr As String() = {"The", "quick", "brown", "fox", _
"jumps", "over", "the", "lazy", "dog"}
' Display the values of the array.
Console.WriteLine( _
"The string array initially contains the following values:")
PrintIndexAndValues(myArr)
' Resize the array to a bigger size (five elements larger).
Array.Resize(myArr, myArr.Length + 5)
' Display the values of the array.
Console.WriteLine("After resizing to a larger size, ")
Console.WriteLine("the string array contains the following values:")
PrintIndexAndValues(myArr)
' Resize the array to a smaller size (four elements).
Array.Resize(myArr, 4)
' Display the values of the array.
Console.WriteLine("After resizing to a smaller size, ")
Console.WriteLine("the string array contains the following values:")
PrintIndexAndValues(myArr)
End Sub
Public Shared Sub PrintIndexAndValues(myArr() As String)
Dim i As Integer
For i = 0 To myArr.Length - 1
Console.WriteLine(" [{0}] : {1}", i, myArr(i))
Next i
Console.WriteLine()
End Sub
End Class
作者:VB.NET开发
项目:Syste
Module Example
Public Sub Main()
Dim arr(9, 1) As Integer
For n1 As Integer = 0 To arr.GetUpperBound(0)
arr(n1, 0) = n1
arr(n1, 1) = n1 * 2
Next
' Make a 2-D array larger in the first dimension.
arr = CType(ResizeArray(arr, { 12, 2} ), Integer(,))
For ctr = 0 To arr.GetUpperBound(0)
Console.WriteLine("{0}: {1}, {2}", ctr, arr(ctr, 0), arr(ctr, 1))
Next
Console.WriteLine()
' Make a 2-D array smaller in the first dimension.
arr = CType(ResizeArray(arr, { 2, 2} ), Integer(,))
For ctr = 0 To arr.GetUpperBound(0)
Console.WriteLine("{0}: {1}, {2}", ctr, arr(ctr, 0), arr(ctr, 1))
Next
End Sub
Private Function ResizeArray(arr As Array, newSizes() As Integer) As Array
If newSizes.Length <> arr.Rank Then
Throw New ArgumentException("arr must have the same number of dimensions " +
"as there are elements in newSizes", "newSizes")
End If
Dim temp As Array = Array.CreateInstance(arr.GetType().GetElementType(), newSizes)
Dim length As Integer = If(arr.Length <= temp.Length, arr.Length, temp.Length )
Array.ConstrainedCopy(arr, 0, temp, 0, length)
Return temp
End Function
End Module